package cc.magicjson.mall.product.domain.model;

import cc.magicjson.mall.common.domain.AbstractAggregateRoot;
import cc.magicjson.mall.product.domain.event.ProductCreatedEvent;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;


import java.math.BigDecimal;

@Entity
@Table(name = "products")
public class Product extends AbstractAggregateRoot<String> {
	@Id
	private String id;

	@Column(nullable = false)
	private String name;

	@Column(length = 1000)
	private String description;

	@Column(nullable = false, precision = 10, scale = 2)
	private BigDecimal price;

	@Column(nullable = false)
	private int stockQuantity;

	protected Product() {} // JPA requires a no-arg constructor

	public Product(String name, String description, BigDecimal price, int stockQuantity) {
		this.id = java.util.UUID.randomUUID().toString();
		this.name = name;
		this.description = description;
		this.price = price;
		this.stockQuantity = stockQuantity;
		registerEvent(new ProductCreatedEvent(this.id, this.name, this.price));
	}

	@Override
	public String getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getDescription() {
		return description;
	}

	public BigDecimal getPrice() {
		return price;
	}

	public int getStockQuantity() {
		return stockQuantity;
	}

	public void updateStock(int quantity) {
		if (this.stockQuantity + quantity < 0) {
			throw new IllegalArgumentException("Insufficient stock");
		}
		this.stockQuantity += quantity;
	}
}